﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ATSBLL;
using System.Drawing;
using System.Collections.Generic;
using ATSModel;

public partial class ViewByProuctSN_ProductSN : System.Web.UI.Page
{
    private PIMTest Ptest;
    private TTest test;
    private Airtightness airtightness;

    private PIMTestInfo pInfo;
    private TTestInfo tInfo;
    private TPackage Tpackage;
    private TPackageInfo TpInfo;
    private AirtightnessInfo AInfo;
    protected void Page_Load(object sender, EventArgs e)
    {
        Ptest = new PIMTest();
        test = new TTest();
        airtightness = new Airtightness();
        pInfo = new PIMTestInfo();
        tInfo = new TTestInfo();
        Tpackage = new TPackage();
        TpInfo = new TPackageInfo();
        AInfo = new AirtightnessInfo();
    }

    /// <summary>
    /// 根据产品序列号查询产品状态
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ButQuery_Click(object sender, EventArgs e)
    {

        //绑定交调测试数据
        BindPTests();
        //绑定电性能测试数据
        BindTests();
        ProductIsPIM();
        ProductIsTest();
        AirtightnessIsPass();
        ProductIsPackage();
        //绑定气密
        BindAirtightness();
        //绑定包装
        BindPackages();

        ImageProcess();
    }

    #region 交调
    /// <summary>
    /// 绑定
    /// </summary>
    public void BindPTests()
    {
        string productSNPT = tbSN.Text.Trim();
        GridView1.DataSource = Ptest.GetTestsbySN(productSNPT);
        GridView1.DataBind();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (GridView1.Rows[i].Cells[11].Text == "Fail")
                GridView1.Rows[i].Cells[11].ForeColor = System.Drawing.Color.Red;
        }
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindPTests();
    }

    /// <summary>
    ///详情
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Mark = "ViewByProuctSN";
        Session["productSN"] = GridView1.SelectedDataKey["ProductSN"].ToString();
        Session["pIMRecordTime"] = Convert.ToDateTime(GridView1.SelectedDataKey["PIMRecordTime"]);
        Session["productTypeID"] = GridView1.SelectedDataKey["ProductTypeID"].ToString();
        Session["userID"] = GridView1.SelectedDataKey["UserID"].ToString();
        Session["pIMENAID"] = GridView1.SelectedDataKey["PIM_ENAID"].ToString();
        Session["pIMPass"] = GridView1.SelectedDataKey["PIMPass"].ToString();
        Response.Redirect("../PIMTest/PIMTestDetails/ViewPIMTestItem.aspx?Mark=" + Mark + "");

        //?ProductSN=" + productSN + "&pIMRecordTime=" + pIMRecordTime + "&ProductTypeID=" + productTypeID + "&UserID=" + userID + "&PIM_ENAID=" + pIMENAID + "&PIMPass=" + pIMPass + "
    }
    #endregion

    #region 电性能
    public void BindTests()
    {
        string productSNT = this.tbSN.Text.Trim();
        this.GridView2.DataSource = test.GetTestsbySN(productSNT);
        this.GridView2.DataBind();
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            if (GridView2.Rows[i].Cells[13].Text == "Fail")
                GridView2.Rows[i].Cells[13].ForeColor = System.Drawing.Color.Red;
        }
    }


    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView2.PageIndex = e.NewPageIndex;
        BindTests();
    }

    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["productSN"] = GridView2.SelectedDataKey["ProductSN"].ToString();
        Session["testTime"] = Convert.ToDateTime(GridView2.SelectedDataKey["RecordTime"]);
        Session["ProductTypeID"] = GridView2.SelectedDataKey["ProductTypeID"].ToString();
        Session["UserID"] = GridView2.SelectedDataKey["UserID"].ToString();
        Session["ENAID"] = GridView2.SelectedDataKey["ENAID"].ToString();
        Session["IsPass"] = GridView2.SelectedDataKey["IsPass"].ToString();

        Response.Redirect("~/TestViewbyProduct/TestDetails/ViewTTestItem.aspx");
    }


    #endregion

    #region 包装
    /// <summary>
    /// 绑定
    /// </summary>
    public void BindPackages()
    {
        string productSN = tbSN.Text.Trim();
        GridView3.DataSource = Tpackage.IsPackageBySN(productSN);
        GridView3.DataBind();
        for (int i = 0; i < GridView3.Rows.Count; i++)
        {
            if (GridView3.Rows[i].Cells[8].Text == "NO")
                GridView3.Rows[i].Cells[8].ForeColor = System.Drawing.Color.Red;
        }
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView3_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView3.PageIndex = e.NewPageIndex;
        BindPackages();
    }

    /// <summary>
    ///详情
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string Mark = "ViewByProuctSN";
        //Session["productSN"] = GridView1.SelectedDataKey["ProductSN"].ToString();
        //Session["pIMRecordTime"] = Convert.ToDateTime(GridView1.SelectedDataKey["PIMRecordTime"]);
        //Session["productTypeID"] = GridView1.SelectedDataKey["ProductTypeID"].ToString();
        //Session["userID"] = GridView1.SelectedDataKey["UserID"].ToString();
        //Session["pIMENAID"] = GridView1.SelectedDataKey["PIM_ENAID"].ToString();
        //Session["pIMPass"] = GridView1.SelectedDataKey["PIMPass"].ToString();
        //Response.Redirect("../PIMTest/PIMTestDetails/ViewPIMTestItem.aspx?Mark=" + Mark + "");

        //?ProductSN=" + productSN + "&pIMRecordTime=" + pIMRecordTime + "&ProductTypeID=" + productTypeID + "&UserID=" + userID + "&PIM_ENAID=" + pIMENAID + "&PIMPass=" + pIMPass + "
    }


    #endregion

    #region 气密
    /// <summary>
    /// 绑定
    /// </summary>
    public void BindAirtightness()
    {
        string productSNPT = tbSN.Text.Trim();
        GridView4.DataSource = airtightness.Airtightness_SelectbySN(productSNPT);
        GridView4.DataBind();
        for (int i = 0; i < GridView4.Rows.Count; i++)
        {
            if (GridView4.Rows[i].Cells[6].Text == "Fail")
                GridView4.Rows[i].Cells[6].ForeColor = System.Drawing.Color.Red;
        }
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView4_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView4.PageIndex = e.NewPageIndex;
        BindAirtightness();
    }

    /// <summary>
    ///详情
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView4_SelectedIndexChanged(object sender, EventArgs e)
    {
    
    
    }
    #endregion


    #region 流程图逻辑

    public void ProductIsPIM()
    {
        string productSN = this.tbSN.Text.Trim();
        pInfo = Ptest.GetTestbySN(productSN);
        if (pInfo.PIMPass != null)
        {
            if (pInfo.PIMPass.Trim().Equals("Pass", StringComparison.OrdinalIgnoreCase))
            {
                this.LinkButton2.BackColor = Color.Green;
                this.Label2.Text = pInfo.PIMRecordTime.ToString();
            }
            else
            {
                this.LinkButton2.BackColor = Color.Red;
                this.Label2.Text = pInfo.PIMRecordTime.ToString();
            }
            this.LinkButton2.Enabled = true;
        }
        else
        {
            this.LinkButton2.BackColor = Color.Empty;
            this.Label2.Text = "";
            this.LinkButton2.Enabled = false;
        }
        this.LinkButton2.Visible = true;
        this.Label2.Visible = true;
    }

    public void ProductIsTest()
    {
        string productSNS = this.tbSN.Text.Trim();
        tInfo = test.GetTestbySN(productSNS);

        if (tInfo.IsPass != null)
        {
            if (tInfo.IsPass.Trim().Equals("Pass", StringComparison.OrdinalIgnoreCase))
            {
                this.LinkButton3.BackColor = Color.Green;
                this.Label3.Text = tInfo.RecordTime.ToString();
            }
            else
            {
                this.LinkButton3.BackColor = Color.Red;
                this.Label3.Text = tInfo.RecordTime.ToString();
            }
            this.LinkButton3.Enabled = true;
        }
        else
        {
            this.LinkButton3.BackColor = Color.Empty;
            this.Label3.Text = "";
            this.LinkButton3.Enabled = false;
        }
        this.LinkButton3.Visible = true;
        this.Label3.Visible = true;
    }

    public void AirtightnessIsPass()
    {
        string productSN = this.tbSN.Text.Trim();
        AInfo = airtightness.AirSelectbySN(productSN);
        if (AInfo.IsPass)
        {
            if (AInfo.IsPass != false)
            {
                this.LinkButton1.BackColor = Color.Green;
                this.Label1.Text = AInfo.RecordTime.ToString();
            }
            else
            {
                this.LinkButton1.BackColor = Color.Red;
                this.Label1.Text = AInfo.RecordTime.ToString();
            }
            this.LinkButton1.Enabled = true;
        }
        else
        {
            this.LinkButton1.BackColor = Color.Empty;
            this.Label1.Text = "";
            this.LinkButton1.Enabled = false;
        }
        this.LinkButton1.Visible = true;
        this.Label1.Visible = true;
    }

    public void ProductIsPackage()
    {
        string productSN = this.tbSN.Text.Trim();
        TpInfo = Tpackage.ProductSN_IsPackage(productSN);
        if (TpInfo.IsPackage != null)
        {
            if (TpInfo.IsPackage == "Yes")
            {
                this.LinkButton4.BackColor = Color.Green;
                this.Label4.Text = TpInfo.PackageTime.ToString();
            }
            else
            {
                this.LinkButton4.BackColor = Color.Red;
                this.Label4.Text = TpInfo.PackageTime.ToString();
            }
            this.LinkButton4.Enabled = true;
        }
        else
        {
            this.LinkButton4.BackColor = Color.Empty;
            this.Label4.Text = "";
            this.LinkButton4.Enabled = false;
        }
        this.LinkButton4.Visible = true;
        this.Label4.Visible = true;
    }






    public void ImageProcess()
    {

        if (this.LinkButton1.BackColor == Color.Green && this.LinkButton2.BackColor == Color.Green)
        {
            this.Image1.ImageUrl = "~/Images/ViewbyProduct/GreenArrow.png";
        }
        else
        {
            this.Image1.ImageUrl = "~/Images/ViewbyProduct/GrayArrow.png";
        }
        if (this.LinkButton2.BackColor == Color.Green && this.LinkButton3.BackColor == Color.Green)
        {
            this.Image2.ImageUrl = "~/Images/ViewbyProduct/GreenArrow.png";
        }
        else
        {
            this.Image2.ImageUrl = "~/Images/ViewbyProduct/GrayArrow.png";
        }

        if (this.LinkButton3.BackColor == Color.Green && this.LinkButton4.BackColor == Color.Green)
        {
            this.Image3.ImageUrl = "~/Images/ViewbyProduct/GreenArrow.png";
        }
        else
        {
            this.Image3.ImageUrl = "~/Images/ViewbyProduct/GrayArrow.png";
        }
        this.Image1.Visible = true;
        this.Image2.Visible = true;
        this.Image3.Visible = true;
    }



    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //Session["productSN"] = this.tbSN.Text.Trim();
        //Session["testTime"] = Convert.ToDateTime(this.Label2.Text);
        //Response.Redirect("~/ViewByProuctSN/ProductSN.aspx");
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        //Session["productSN"] = this.tbSN.Text.Trim();
        //Session["testTime"] = Convert.ToDateTime(this.Label3.Text);
        //Response.Redirect("~/ViewByProuctSN/ProductSN.aspx"); 
    }


    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        //Session["productSN"] = this.tbSN.Text.Trim();
        //Session["testTime"] = Convert.ToDateTime(this.Label3.Text);
        //Response.Redirect("~/ViewByProuctSN/ProductSN.aspx"); 
    }

    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        //Session["productSN"] = this.tbSN.Text.Trim();
        //Session["testTime"] = Convert.ToDateTime(this.Label3.Text);
        //Response.Redirect("~/ViewByProuctSN/ProductSN.aspx"); 
    }
    #endregion
}
